package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.ISessionBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.model.PageData;
import com.cloudservice.ccs.entity.Session;
import com.cloudservice.ccs.model.SessionWithLatestMessage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/session")
public class SessionController {

    @Autowired
    private ISessionBiz sessionBiz;

    @PostMapping("/")
    public ApiResult save(@RequestBody Session session) {
        sessionBiz.save(session);
        return ApiResult.success();
    }

    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody Session session) {
        sessionBiz.update(id, session);
        Session result = sessionBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        sessionBiz.partialUpdate(id, fields);
        Session result = sessionBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        sessionBiz.delete(id);
        return ApiResult.success();
    }

    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        Session result = sessionBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Session> result = sessionBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<Session>) result));
    }

    @GetMapping("/customer-service/{customerServiceId}")
    public ApiResult queryAllByCustomerServiceId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) {
        PageHelper.startPage(page, limit);
        List<SessionWithLatestMessage> result = sessionBiz.getAllByCustomerServiceId(customerServiceId);
        return ApiResult.successWithData(PageData.fromPage((Page<SessionWithLatestMessage>) result));
    }

    @GetMapping("/customer-service/{customerServiceId}/active")
    public ApiResult queryActiveByCustomerServiceId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) {
        PageHelper.startPage(page, limit);
        List<SessionWithLatestMessage> result = sessionBiz.getActiveByCustomerServiceId(customerServiceId);
        return ApiResult.successWithData(PageData.fromPage((Page<SessionWithLatestMessage>) result));
    }

    @GetMapping("/customer-service/{customerServiceId}/history")
    public ApiResult queryHistoryByCustomerServiceId(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit, @PathVariable Integer customerServiceId) {
        PageHelper.startPage(page, limit);
        List<SessionWithLatestMessage> result = sessionBiz.getHistoryByCustomerServiceId(customerServiceId);
        return ApiResult.successWithData(PageData.fromPage((Page<SessionWithLatestMessage>) result));
    }

    @GetMapping("/customer-service/{customerServiceId}/visitor/{visitorIp}")
    public ApiResult queryByCustomerServiceAndVisitor(@PathVariable Integer customerServiceId, @PathVariable String visitorIp) {
        Session session = sessionBiz.getByCustomerServiceAndVisitor(customerServiceId, visitorIp);
        return ApiResult.successWithData(session);
    }
}
